// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Bridge Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 44 : TYPE_BRIDGE

// Function declarations
reserve function Bridge_DebugDraw
reserve function Bridge_DebugSpawn

// Static Values
public value Bridge_value32 = 0;

// Tables

function Bridge_DebugDraw
	temp0 = object[0].value17
	temp0 -= Bridge_value32
	temp2 = temp0
	temp0 <<= 19
	FlipSign(temp0)
	temp0 += object.xpos
	temp0 += 0x80000
	temp1 = 0
	while temp1 < temp2
		DrawSpriteXY(0, temp0, object.ypos)
		temp0 += 0x100000
		temp1++
	loop
	if temp1 == 0
		DrawSprite(0)
	end if
end function


function Bridge_DebugSpawn
	temp0 = object[0].value17
	temp0 -= Bridge_value32
	CreateTempObject(TypeName[Bridge], temp0, object.xpos, object.ypos)
	object[tempObjectPos].value6 = temp0
	object[tempObjectPos].value6 <<= 19
	object[tempObjectPos].value7 = object[tempObjectPos].value6
	FlipSign(object[tempObjectPos].value6)
	object[tempObjectPos].value6 += object[tempObjectPos].xpos
	object[tempObjectPos].value7 += object[tempObjectPos].xpos
	object[tempObjectPos].value5 = -1
end function


event ObjectMain
	if object.value0 > 0
		if object.value1 < 128
			object.value1 += 8
		end if
	else
		if object.value1 > 0
			object.value1 -= 8
			object.value5 = -1
		else
			object.value3 = 0
		end if
	end if
	object.value4 = object.value3
	object.value4 *= object.value1
	object.value4 >>= 7
	object.value0 = 0
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if object[currentPlayer].xpos > object.value6
			if object[currentPlayer].xpos < object.value7
				if currentPlayer == object.value5
					object.value2 = object[currentPlayer].xpos
					object.value2 -= object.value6
					temp0 = object.value2
					temp0 >>= 8
					temp1 = object.value7
					temp1 -= object.value6
					temp2 = temp1
					temp2 >>= 16
					temp0 /= temp2
					Sin(object.value3, temp0)
					temp1 >>= 13
					object.value3 *= temp1
					temp0 = object.ypos
					temp0 -= 0x300000
					if object[currentPlayer].ypos > temp0
						if object[currentPlayer].yvel >= 0
							temp2 = object[currentPlayer].collisionBottom
							FlipSign(temp2)
							temp2 <<= 16
							temp2 += object.value4
							temp2 -= 0x80000
							object.value0++
							object[currentPlayer].ypos = object.ypos
							object[currentPlayer].ypos += temp2
							object[currentPlayer].gravity = GRAVITY_GROUND
							object[currentPlayer].yvel = 0
							object[currentPlayer].floorSensorL = 1
							object[currentPlayer].floorSensorC = 1
							object[currentPlayer].floorSensorR = 1
						else
							object.value5 = -2
						end if
					end if
				else
					if object[currentPlayer].yvel >= 0
						temp0 = object[currentPlayer].xpos
						temp0 -= object.value6
						if temp0 > object.value2
							temp0 = object.value7
							temp0 -= object[currentPlayer].xpos
							temp3 = object.value7
							temp3 -= object.value6
							temp3 -= object.value2
							temp1 = temp0
							temp1 <<= 7
							temp1 /= temp3
						else
							temp1 = temp0
							temp1 <<= 7
							temp1 /= object.value2
						end if
						Sin(temp2, temp1)
						temp2 *= object.value4
						temp2 >>= 9
						temp2 -= 0x80000
						if object[currentPlayer].yvel < 0x8000
							temp3 = temp2
							temp3 >>= 16
							temp4 = temp3
							temp3 -= 8
						else
							temp3 = temp2
							temp3 >>= 16
							temp4 = temp3
							temp4 += 8
						end if
						BoxCollisionTest(C_TOUCH, object.entityPos, -0x400, temp3, 0x400, temp4, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
						if checkResult == 1
							object.value0++
							object[currentPlayer].ypos = object[currentPlayer].collisionBottom
							FlipSign(object[currentPlayer].ypos)
							object[currentPlayer].ypos <<= 16
							object[currentPlayer].ypos += object.ypos
							object[currentPlayer].ypos += temp2
							object[currentPlayer].floorSensorL = 1
							object[currentPlayer].floorSensorC = 1
							object[currentPlayer].floorSensorR = 1
							if currentPlayer == 0
								if object.value5 < 4
									if object.value5 > 0
										object.value2 = object[currentPlayer].xpos
										object.value2 -= object.value6
										temp0 = object.value2
										temp0 >>= 8
										temp1 = object.value7
										temp1 -= object.value6
										temp2 = temp1
										temp2 >>= 16
										temp0 /= temp2
										Sin(object.value3, temp0)
										temp1 >>= 13
										object.value3 *= temp1
										object.value4 = object.value3
										object.value4 *= object.value1
										object.value4 >>= 7
									end if
									object.value5 = 0
									if object[currentPlayer].yvel < 256
										object.value1 = 128
									end if
								end if
							else
								if object.value5 == -1
									object.value5 = currentPlayer
									if object[currentPlayer].yvel < 256
										object.value1 = 128
									end if
								end if
								if object.value5 == -2
									object.value5 = currentPlayer
								end if
							end if
							object[currentPlayer].gravity = GRAVITY_GROUND
							object[currentPlayer].yvel = 0
						end if
					end if
				end if
			else
				if currentPlayer == object.value5
					object.value5 = -2
					object.value1 = 32
				end if
			end if
		else
			if currentPlayer == object.value5
				object.value5 = -2
				object.value1 = 32
			end if
		end if
	next
end event


event ObjectDraw
	temp0 = 0
	temp1 = object.value6
	temp1 += 0x80000
	temp4 = 0x80000
	temp5 = object.value2
	temp5 >>= 20
	while temp0 < temp5
		temp3 = temp4
		temp3 <<= 7
		temp3 /= object.value2
		Sin(temp2, temp3)
		temp2 *= object.value4
		temp2 >>= 9
		temp2 += object.ypos
		DrawSpriteXY(0, temp1, temp2)
		temp1 += 0x100000
		temp4 += 0x100000
		temp0++
	loop
	temp2 = object.value4
	temp2 += object.ypos
	DrawSpriteXY(0, temp1, temp2)
	temp1 += 0x100000
	temp0++
	temp5 = object.value7
	temp5 -= object.value6
	temp5 -= object.value2
	temp1 = object.value7
	temp1 -= 0x80000
	temp4 = 0x80000
	while temp0 < object.propertyValue
		temp3 = temp4
		temp3 <<= 7
		temp3 /= temp5
		Sin(temp2, temp3)
		temp2 *= object.value4
		temp2 >>= 9
		temp2 += object.ypos
		DrawSpriteXY(0, temp1, temp2)
		temp1 -= 0x100000
		temp4 += 0x100000
		temp0++
	loop
end event


event ObjectStartup
	LoadSpriteSheet("GHZ/Objects.gif")
	SpriteFrame(-8, -8, 16, 16, 1, 1)
	foreach (TypeName[Bridge], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value6 = object[arrayPos0].propertyValue
		object[arrayPos0].value6 <<= 19
		object[arrayPos0].value7 = object[arrayPos0].value6
		FlipSign(object[arrayPos0].value6)
		object[arrayPos0].value6 += object[arrayPos0].xpos
		object[arrayPos0].value7 += object[arrayPos0].xpos
		object[arrayPos0].value5 = -1
	next
	temp0 = 0
	Bridge_value32 = DebugMode_ObjCount
	Bridge_value32--
	while temp0 < 16
		SetTableValue(TypeName[Bridge], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(Bridge_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(Bridge_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
